package com.sh3h.generator.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.sh3h.generator.web.domain.GenFtl;
import com.sh3h.generator.web.dtos.GenFtlDto;
import com.sh3h.generator.web.dtos.Result;
import com.sh3h.generator.web.service.impl.GenFtlServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"/api/gen/ftls"})
public class FTLController {
    @Autowired
    private GenFtlServiceImpl fieldTypeService;

    @GetMapping("")
    public Result getByPage(int pageNum, int pageSize, String ftlGroup, String ftlName, String ftlType) {
        Page<GenFtl> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<GenFtl> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(ftlGroup)) {
            queryWrapper.like(GenFtl::getFtlGroup, ftlGroup);
        }
        if (!StringUtils.isNullOrEmpty(ftlName)) {
            queryWrapper.like(GenFtl::getFtlName, ftlName);
        }
        if (!StringUtils.isNullOrEmpty(ftlType)) {
            queryWrapper.like(GenFtl::getFtlType, ftlType);
        }
        Page<GenFtl> datasourceList = fieldTypeService.page(page, queryWrapper);
        return Result.success(datasourceList);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id) {
        GenFtl datasource = fieldTypeService.getById(id);
        return Result.success(datasource);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id) {
        Boolean result = fieldTypeService.removeById(id);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    public Result updateById(@RequestBody GenFtlDto dto) {
        GenFtl datasource = GenFtlDto.ToModal(dto);
        Boolean result = fieldTypeService.updateById(datasource);
        return Result.success(result);
    }

    @PostMapping("/{id}")
    public Result insert(@RequestBody GenFtlDto dto) {
        GenFtl datasource = GenFtlDto.ToModal(dto);
        Boolean result = fieldTypeService.save(datasource);
        return Result.success(result);
    }
}
